using micromsg;
using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Network;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Scene.Voice;
using System;
using System.IO;
namespace MicroMsg.Plugin.MassSend.Scene
{
	public class NetSceneMassSend : NetSceneBaseEx<MassSendRequest, MassSendResponse, MassSendRequest.Builder>
	{
		public class NetSceneMassSendResult
		{
			public RetConst retCode;
			public bool isVoiceTooShort;
		}
		private const string TAG = "NetSceneMassSend";
		private MassSendContext mMassSendContext;
		public bool doSceneSendText(string strUserNameList, uint nUserCount, bool bIsSendAgain, string strContent)
		{
			if (string.IsNullOrEmpty(strUserNameList) || nUserCount == 0u || string.IsNullOrEmpty(strContent))
			{
				Log.smethod_1("NetSceneMassSend", "invalid input parameters");
				return false;
			}
			if (this.mMassSendContext != null && this.mMassSendContext.isRunning())
			{
				Log.smethod_1("NetSceneMassSend", "isRunning now , please retry later");
				return false;
			}
			this.mMassSendContext = new MassSendContext();
			if (!this.mMassSendContext.packText(strUserNameList, nUserCount, bIsSendAgain, strContent))
			{
				Log.smethod_1("NetSceneMassSend", "packText failed ");
				this.mMassSendContext.mMassMsgInfo.nStatus = 5;
				return false;
			}
			if (!this.doSceneEx())
			{
				Log.smethod_1("NetSceneMassSend", "doSceneEx failed ");
				this.mMassSendContext.mMassMsgInfo.nStatus = 5;
				return false;
			}
			this.mMassSendContext.mMassMsgInfo.nStatus = 2;
			return true;
		}
		public bool doSceneSendImg(string strUserNameList, uint nUserCount, bool bIsSendAgain, Stream origStream, bool isNeedScale = true)
		{
			if (string.IsNullOrEmpty(strUserNameList) || nUserCount == 0u || origStream == null)
			{
				Log.smethod_1("NetSceneMassSend", "invalid input parameters");
				return false;
			}
			if (this.mMassSendContext != null && this.mMassSendContext.isRunning())
			{
				Log.smethod_1("NetSceneMassSend", "isRunning now , please retry later");
				return false;
			}
			this.mMassSendContext = new MassSendContext();
			if (!this.mMassSendContext.packImage(strUserNameList, nUserCount, bIsSendAgain, origStream, isNeedScale))
			{
				Log.smethod_1("NetSceneMassSend", "packImage failed ");
				this.mMassSendContext.mMassMsgInfo.nStatus = 5;
				return false;
			}
			if (!this.doSceneEx())
			{
				Log.smethod_1("NetSceneMassSend", "doSceneEx failed ");
				this.mMassSendContext.mMassMsgInfo.nStatus = 5;
				return false;
			}
			this.mMassSendContext.mMassMsgInfo.nStatus = 2;
			return true;
		}
		public bool doSceneBeginSendVoice()
		{
			if (this.mMassSendContext != null && this.mMassSendContext.isRunning())
			{
				Log.smethod_1("NetSceneMassSend", "isRunning now , please retry later");
				return false;
			}
			if (RecorderVoice.isRunning())
			{
				Log.smethod_1("NetSceneMassSend", "RecorderVoice isRunning now , please retry later");
				return false;
			}
			this.mMassSendContext = new MassSendContext();
			return RecorderVoice.start(this.mMassSendContext, null);
		}
		public bool doSceneEndSendVoice(string strUserNameList, uint nUserCount, bool bIsSendAgain, bool bIsCancelled)
		{
			if (string.IsNullOrEmpty(strUserNameList) || nUserCount == 0u)
			{
				Log.smethod_1("NetSceneMassSend", "invalid input parameters");
				this.mMassSendContext.mMassMsgInfo.nStatus = 5;
				return false;
			}
			if (!RecorderVoice.isRunning())
			{
				Log.smethod_1("NetSceneMassSend", "RecorderVoice cannot end without begin, ignored. ");
				this.mMassSendContext.mMassMsgInfo.nStatus = 5;
				return false;
			}
			this.mMassSendContext.mMassMsgInfo.strUserNameList = strUserNameList;
			this.mMassSendContext.mMassMsgInfo.nUserCount = nUserCount;
			this.mMassSendContext.mMassMsgInfo.bIsSendAgain = bIsSendAgain;
			this.mMassSendContext.bIsCancelled = bIsCancelled;
			this.mMassSendContext.mOnStop += new MassSendContext.onStopDelegate(this.onStop);
			if (!RecorderVoice.stop())
			{
				Log.smethod_1("NetSceneMassSend", "RecorderVoice cannot stop, ignored. ");
				this.mMassSendContext.mMassMsgInfo.nStatus = 5;
				return false;
			}
			return true;
		}
		public void onStop(byte[] voiceBuffer, int dataLength, int timeLength)
		{
			if (this.mMassSendContext.bIsCancelled)
			{
				Log.smethod_5("NetSceneMassSend", "voice msg has been cancelled");
				this.mMassSendContext.mMassMsgInfo.nStatus = 4;
				return;
			}
			if (timeLength < 1000)
			{
				Log.smethod_1("NetSceneMassSend", "voice too short");
				this.mMassSendContext.mMassMsgInfo.nStatus = 5;
				EventCenter.postEvent(EventConst.ON_NETSCENE_MASS_SEND_ERR, new NetSceneMassSend.NetSceneMassSendResult
				{
					retCode = RetConst.MM_ERR_CLIENT,
					isVoiceTooShort = true
				}, null);
				return;
			}
			if (!this.mMassSendContext.packVoice(voiceBuffer, dataLength, timeLength))
			{
				Log.smethod_1("NetSceneMassSend", "packVoice failed ");
				this.mMassSendContext.mMassMsgInfo.nStatus = 5;
				EventCenter.postEvent(EventConst.ON_NETSCENE_MASS_SEND_ERR, new NetSceneMassSend.NetSceneMassSendResult
				{
					retCode = RetConst.MM_ERR_CLIENT
				}, null);
				return;
			}
			if (!this.doSceneEx())
			{
				Log.smethod_1("NetSceneMassSend", "doSceneEx failed ");
				this.mMassSendContext.mMassMsgInfo.nStatus = 5;
				EventCenter.postEvent(EventConst.ON_NETSCENE_MASS_SEND_ERR, new NetSceneMassSend.NetSceneMassSendResult
				{
					retCode = RetConst.MM_ERR_CLIENT
				}, null);
				return;
			}
			this.mMassSendContext.mMassMsgInfo.nStatus = 2;
		}
		private bool doSceneEx()
		{
			this.mMassSendContext.mTimesToServer += 1u;
			Log.smethod_4("NetSceneMassSend", "doscene in, mMassSendContext.mTimesToServer = " + this.mMassSendContext.mTimesToServer);
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.ToList = this.mMassSendContext.mMassMsgInfo.strUserNameList;
			this.mBuilder.ToListMD5 = MD5Core.GetHashString(this.mMassSendContext.mMassMsgInfo.strUserNameList);
			this.mBuilder.ClientID = this.mMassSendContext.mMassMsgInfo.strClientMsgId;
			this.mBuilder.MsgType = this.mMassSendContext.mMassMsgInfo.nMsgType;
			this.mBuilder.MediaTime = (uint)this.mMassSendContext.mMassMsgInfo.nMediaTime;
			this.mBuilder.ToListCount = this.mMassSendContext.mMassMsgInfo.nUserCount;
			this.mBuilder.IsSendAgain = (this.mMassSendContext.mMassMsgInfo.bIsSendAgain ? 1u : 0u);
			if (this.mMassSendContext.mMassMsgInfo.nMsgType == 3u)
			{
				this.mBuilder.CompressType = (this.mMassSendContext.mMassMsgInfo.isNeedScale ? 0u : 1u);
			}
			if (this.mMassSendContext.mMassMsgInfo.nMsgType == 43u)
			{
				Log.smethod_1("NetSceneMassSend", "have not supported yet，nMsgType=" + 43);
				return false;
			}
			this.mBuilder.DataBuffer = this.mMassSendContext.getRemainDataToSend();
			if (this.mBuilder.DataBuffer.Buffer.Length == 0)
			{
				Log.smethod_1("NetSceneMassSend", "getRemainDataToSend failed");
				return false;
			}
			this.mBuilder.DataStartPos = (uint)this.mMassSendContext.mStartPos;
			this.mBuilder.DataTotalLen = this.mMassSendContext.mMassMsgInfo.nDataTotalLength;
			this.mBuilder.ThumbData = Util.toSKBuffer("");
			this.mBuilder.ThumbStartPos = 0u;
			this.mBuilder.ThumbTotalLen = 0u;
			this.mBuilder.CameraType = 0u;
			this.mBuilder.VideoSource = 0u;
			this.mSessionPack.mCmdID = 84;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(MassSendRequest request, MassSendResponse response)
		{
			this.mMassSendContext.mMassMsgInfo.nStatus = 3;
			PackResult lastError = base.getLastError();
			Log.smethod_4("NetSceneMassSend", "onFailed in PackResult =" + lastError);
			if (lastError == PackResult.BEEN_CANCELLED)
			{
				return;
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_MASS_SEND_ERR, new NetSceneMassSend.NetSceneMassSendResult
			{
				retCode = RetConst.MM_ERR_CLIENT
			}, null);
		}
		protected override void onSuccess(MassSendRequest request, MassSendResponse response)
		{
			Log.smethod_4("NetSceneMassSend", "onSuccess in");
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneMassSend", "send MassSendRequest failed ret =" + ret);
				this.mMassSendContext.mMassMsgInfo.nStatus = 3;
				EventCenter.postEvent(EventConst.ON_NETSCENE_MASS_SEND_ERR, new NetSceneMassSend.NetSceneMassSendResult
				{
					retCode = ret
				}, null);
				return;
			}
			if (this.mMassSendContext.mTimesToServer > 30u)
			{
				Log.smethod_1("NetSceneMassSend", "reach the max times, mTimesToServer = " + this.mMassSendContext.mTimesToServer);
				this.mMassSendContext.mMassMsgInfo.nStatus = 3;
				EventCenter.postEvent(EventConst.ON_NETSCENE_MASS_SEND_ERR, new NetSceneMassSend.NetSceneMassSendResult
				{
					retCode = RetConst.MM_ERR_CLIENT
				}, null);
				return;
			}
			if (!this.mMassSendContext.isSendCompleted())
			{
				if (!this.doSceneEx())
				{
					Log.smethod_1("NetSceneMassSend", "doSceneEx failed ");
					this.mMassSendContext.mMassMsgInfo.nStatus = 5;
					EventCenter.postEvent(EventConst.ON_NETSCENE_MASS_SEND_ERR, new NetSceneMassSend.NetSceneMassSendResult
					{
						retCode = RetConst.MM_ERR_CLIENT
					}, null);
				}
				return;
			}
			Log.smethod_5("NetSceneMassSend", "send MassSendRequest complete");
			this.mMassSendContext.mMassMsgInfo.nTime = (long)Util.getNowSeconds();
			this.mMassSendContext.mMassMsgInfo.nStatus = 4;
			if (!this.mMassSendContext.saveInfo())
			{
				Log.smethod_1("NetSceneMassSend", "send MassSendRequest success,but save info failed");
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_MASS_SEND_SUCCESS, null, null);
		}
	}
}
